<?php
	$matt=$_POST['matt'];
	/*if($_POST['danhdau']==0)
	{
		$tam=basename( $_FILES['anh']['name']);
			$tam=explode("%",$tam);
			$tam=implode("",$tam);
			$tam=explode(" ",$tam);
			$tam=implode("",$tam);
			$st=explode(".",$tam);
			$tam=substr($tam,1,9).".".$st[1];
			$duongdan ="upload_images\/".$tam;
			echo "duongdan: ".$duongdan;
			if(@move_uploaded_file($_FILES['anh']['tmp_name'], $duongdan)) 
		   {
			  $result = $duongdan;
			  $knoi->queryCsdl("insert into hinhanh values('','$result','','$matt')");
		   }
	}
	else
	{*/
		 $path_thumbs = "upload_images";
			//the new width of the resized image, in pixels.
			$img_thumb_width = 200; // 
	
			$extlimit = "yes"; //Limit allowed extensions? (no for all extensions allowed)
			//List of allowed extensions if extlimit = yes
			$limitedext = array(".gif",".jpg",".png",".jpeg",".bmp");
		for($i=1;$i<=$slanh;$i++)
		{
			//echo "<br>i: ".$i."<br>";
			$tam=basename( $_FILES['anh'.$i]['name']);
			//echo "fgdk".$tam;
			$tam=explode("%",$tam);
			$tam=implode("",$tam);
			$tam=explode(" ",$tam);
			$tam=implode("",$tam);
			$result='';
			$resize='';
			//echo "<br>".$_FILES['anh'.$i]['name'];
			$duongdan ="upload_images\/".$tam;
			//the image -> variables
			$file_type = $_FILES['anh'.$i]['type'];
			$file_name = $_FILES['anh'.$i]['name'];
			$file_size = $_FILES['anh'.$i]['size'];
			$file_tmp = $_FILES['anh'.$i]['tmp_name'];
			$ext = strrchr($file_name,'.');
		   $ext = strtolower($ext);
		   /*if (($extlimit == "yes") && (!in_array($ext,$limitedext))) 
		   {
			  echo "Wrong file extension.  <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";
			  exit();
		   }*/
		   //so, whats the file's extension?
		   $getExt = explode ('.', $file_name);
		   $file_ext = $getExt[count($getExt)-1];
		   $rand_name = md5(time());
		   $rand_name= rand(0,999999999);
		   $ThumbWidth = $img_thumb_width;
		   if($file_size)
		   {
			  if($file_type == "image/pjpeg" || $file_type == "image/jpeg")
			  {
				   $new_img = imagecreatefromjpeg($file_tmp);
			   }
			   elseif($file_type == "image/x-png" || $file_type == "image/png")
			   {
				   $new_img = imagecreatefrompng($file_tmp);
			   }
			   elseif($file_type == "image/gif")
			   {
				   $new_img = imagecreatefromgif($file_tmp);
			   }
			   //list the width and height and keep the height ratio.
			   list($width, $height) = getimagesize($file_tmp);
			   //calculate the image ratio
			   $imgratio=$width/$height;
			   if ($imgratio>1)
			   {
				  $newwidth = $ThumbWidth;
				  $newheight = $ThumbWidth/$imgratio;
			   }
			   else
			   {
					 $newheight = $ThumbWidth;
					 $newwidth = $ThumbWidth*$imgratio;
			   }
			   //function for resize image.
			   if (function_exists(imagecreatetruecolor))
			   {
			   $resized_img = imagecreatetruecolor($newwidth,$newheight);
			   }
			   else
			   {
					 die("Error: Please make sure you have GD library ver 2+");
			   }
			   //the resizing is going on here!
			   imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
			   //finally, save the image
			   ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext");
			   ImageDestroy ($resized_img);
			   ImageDestroy ($new_img);
			}
			if(@move_uploaded_file($_FILES['anh'.$i]['tmp_name'], $duongdan)) 
		   {
		   		@move_uploaded_file ($file_tmp, "$path_thumbs/$rand_name.$file_ext");
			  $result = $duongdan;
			  $resize="$path_thumbs/$rand_name.$file_ext";
			  $knoi->queryCsdl("insert into hinhanh values('','$result','$resize','','$matt')");
		   }
		}
		$matt=$_POST['matt'];
		$maloaitin=$_POST['maloaitt'];
		$tieude=$_POST['tieude'];
		$tomluoc=$_POST['tomluoc'];
		$noidung=$_POST['noidung'];
		$trangthai=$_POST['trangthai'];
		$slanh=$_POST['slanh'];
		$date=date('Y-m-d');
		$query="update tintuc set tieude='$tieude',tomluoc='$tomluoc',noidung='$noidung',trangthai='$trangthai',maloaitt='$maloaitin' where matt='$matt'";
		if($trangthai==1)
			$query="update tintuc set tieude='$tieude',tomluoc='$tomluoc',noidung='$noidung',ngaydang=NOW(),trangthai='$trangthai',maloaitt='$maloaitin' where matt='$matt'";
		$i=1;
		if($knoi->queryCsdl($query))
		{
			echo "<script language='javascript'>alert('Đã cập nhật tin tức thành công!');</script>";
			echo "<meta http-equiv='refresh' content='0;url=home_admin.php?module=chitiettintuc&matt=".$matt."' />";
		}
		else
		{
			echo "<script language='javascript'>alert('Có lỗi trong quá trình cập nhật!');</script>";
			echo "<meta http-equiv='refresh' content='0;url=home_admin.php?module=chitiettintuc&matt=".$matt."&tieude=".$tieude."&tomluoc=".$tomluoc."&noidung=".$noidung."'/>";
		}
	//}
	?>
	
